<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body,
        html {
            margin: 0 auto;
            padding: 0;
            width: 100%;
            height: 100%;
            background-color: lightseagreen;
            overflow: hidden;
        }

        * {
            box-sizing: border-box;
        }

        .layout {
            height: 100%;
            width: 100%;
            overflow: hidden;
            display: flex;
        }

        .layout-side {
            height: 100%;
            width: 200px;
            background-color: blue;
            border-right: 1px red solid;
        }

        .layout-main {
            height: 100%;
            width: 100%;
            background-color: #e4d399;
            flex-grow: 1;
        }

        .img_each_container {
            margin-top: 1px;
            background-color: lightseagreen;
        }

        .debug_tools_container {
            width: 400px;
            height: 50px;
            line-height: 50px;
            padding: 0px 10px;
            background-color: lightslategray;
            position: absolute;
            bottom: 100px;
            left: 200px;
        }

        .debug_tools_container a {
            padding: 4px 10px;
            background-color: lightseagreen;
            color: white;
        }
    </style>
    <script src="https://cdn.bootcss.com/fabric.js/3.2.0/fabric.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>

    <div class="layout">

        <div class="layout-side">

            <div class="img_each_container">
                <img src="200x200.png" id="img_01" style="width:100px;height:100px;">
            </div>

            <div class="img_each_container">
                <img src="200x200.png" id="img_01" style="width:100px;height:100px;">
            </div>

        </div>
        <div class="layout-main">
            <canvas id="myCanvas"></canvas>
        </div>

        <div class="debug_tools_container">
            <a href="###" id="getJson">获取JSON</a>
            <a href="###" id="loadJson">加载JSON</a>

            <a href="###" id="scaleDown">放大</a>

            <a href="###" id="scaleUp">缩小</a>

        </div>


    </div>

    <script>
        function printf() {
            console.log(arguments);
        }

        var windowOption = {
            currentCanvas: {
                width: 0,
                height: 0
            }
        };

        window.onload = function () {

        }

        $(function () {



            var canvas_width = $(".layout-main").width();
            var canvas_height = $(".layout-main").height();

            console.log(canvas_width, "canvas_width");
            console.log(canvas_height, "canvas_height");

            var c = document.getElementById("myCanvas");
            c.width = canvas_width;
            c.height = canvas_height;

            var canvas = new fabric.Canvas('myCanvas',{
                selectionColor:'blue'
            });
            canvas.width = canvas_width;
            canvas.height = canvas_height;




            var drawItem = {
                width: 1000,
                height: 350
            }

            var bg = new fabric.Image.fromURL('./bg_black.png', function (oImg) {
                oImg.left = (canvas.width - drawItem.width) / 2;
                oImg.top = (canvas.height - drawItem.height) / 2;
                oImg.width = 1000;
                oImg.height = 350;
                oImg.angle = 0; //设置旋;

                oImg.lockMovementX = true;
                oImg.lockMovementY = true;

                oImg.hasControls = false;
                oImg.hasBorders = false;

                oImg.selectable = false;
                //canvas.add(oImg);
                canvas.insertAt(oImg, 0);
            });



            canvas.on('object:modified', (e) => {
                console.log(e.target, "=========object:modified============") // e.target为当前编辑的Object
                // ...旋转，缩放，移动等编辑图层的操作都监听到
                // 所以如果有撤销/恢复的场景，这里可以保存编辑状态
            });



            $(".layout-main").bind('mouseenter', function (e) {

                var dom = e.relatedTarget;

                if (e.relatedTarget == null) {
                    return;
                }

                var imgUrl = $(e.relatedTarget).attr('src');

                if (imgUrl == null) {
                    return;
                }


                var fromResData = {
                    dom: e.relatedTarget,
                    width: $(e.relatedTarget).width(),
                    height: $(e.relatedTarget).height()
                }

                console.log(fromResData, "=========fromResData===========");

                var imgInstance = new fabric.Image(e.relatedTarget, { //设置图片在canvas中的位置和样子
                    left: e.offsetX - fromResData.width / 2,
                    top: e.offsetY - fromResData.height / 2,
                    width: fromResData.width,
                    height: fromResData.height,
                    angle: 0, //设置旋转
                    // hasControls:false,
                    // hasBorders:false
                });
                canvas.add(imgInstance); //加入到canvas中




            });


            // $("#scaleDown").click(function () {
            //     canvas.setZoom(canvas.getZoom() * 1.2);
            // });

            canvas.on('mouse:wheel', function (opt) {

                var delta = opt.e.deltaY;
                var pointer = canvas.getPointer(opt.e);
                var zoom = canvas.getZoom();
                zoom = zoom + delta / 1000;
                if (zoom > 20) zoom = 20;
                if (zoom < 0.01) zoom = 0.01;
                canvas.zoomToPoint({
                    // x: opt.e.offsetX,
                    // y: opt.e.offsetY
                    x: canvas.getCenter().top,
                    y: canvas.getCenter().left
                }, zoom);
                opt.e.preventDefault();
                opt.e.stopPropagation();
            });



        });
    </script>

</body>

</html>